package com.hengyi.pda.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.base.Constants;
import com.common.bean.SubmitListBagsBean;
import com.common.broadcastRecever.ScanDataBroadcastReceiver;
import com.common.cons.Constans;
import com.common.listener.OnOnceClickListener;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.DataCenter;
import com.common.retrofit.ShuangTu.QueryResultBean;
import com.common.retrofit.base.ShuangTuMethods;
import com.common.retrofit.service.ShuangTuService;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.ActivityStack;
import com.common.utils.EmptyUtils;
import com.common.utils.LogUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.dialog.CommonDialog;
import com.common.widget.navigation.WidgetButton;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hengyi.pda.R;
import com.hengyi.pda.adapter.ScanAdapter;
import com.hengyi.pda.bean.ScanIntrueBean;
import com.hengyi.pda.submitjson.SheetSubmitBean;
import com.hengyi.pda.utils.NumberUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;
import rx.functions.Action1;

public class ScanBagsIntrueActivity extends BaseMvpActivity {
    public static final String BEAN = "bean";
    public static final String MADANNUM = "madancode";
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.tv_yisao)
    TextView tvYisao;
    @BindView(R.id.tv_update)
    TextView tvUpdate;

    private QueryResultBean bean;
    private ScanDataBroadcastReceiver scanDataBroadcastReceiver;
    private ScanAdapter adapter;
    TreeSet tree = new TreeSet();
    ScanIntrueBean scanIntrueBean = new ScanIntrueBean();
    private ScanIntrueBean.TablesBean tablesBeans;
    private String madanhao;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan_bags_intrue;
    }

    @Override
    protected void onViewCreated() {
        setRightNavigationText();
    }

    @Override
    protected void onViewClicked(View view) {

        if (view.getId() == tvSubmit.getId()) {
            scanIntrueBean.tables = tablesBeans;
            gotoSubmit();

        } else if (view.getId() == tvUpdate.getId()) {
            gotoUpdate();
        }
    }

    private void gotoUpdate() {
        showProgressingDialog();
        CommonSubscriber<QueryResultBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                final QueryResultBean updateBean = (QueryResultBean) o;
                if (EmptyUtils.isNotEmpty(updateBean) && EmptyUtils.isNotEmpty(updateBean.exports) && updateBean.exports.E_RETURN.equals("1")) {

                    if (EmptyUtils.isNotEmpty(bean) && EmptyUtils.isNotEmpty(bean.tables.T_NOTICE)
                            && bean.tables.T_NOTICE.size() > 0
                    ) {

                /*        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
                            bean.tables.T_NOTICE.get(i).WEIGHT = updateBean.tables.T_NOTICE.get(i).WEIGHT;
                        }*/

                        /****************更新最新得码单数量*****************/
                        for (int i = 0; i < updateBean.tables.T_NOTICE.size(); i++) {
                            for (int j = 0; j < bean.tables.T_NOTICE.size(); j++) {
//                                bean.tables.T_NOTICE.get(i).WEIGHT = updateBean.tables.T_NOTICE.get(i).WEIGHT;

                                if (bean.tables.T_NOTICE.get(j).CODE_SPEC.equals(updateBean.tables.T_NOTICE.get(i).CODE_SPEC)
                                        && bean.tables.T_NOTICE.get(j).CODE_BATCH.equals(updateBean.tables.T_NOTICE.get(i).CODE_BATCH)
                                        && bean.tables.T_NOTICE.get(j).CODE_GRADE.equals(updateBean.tables.T_NOTICE.get(i).CODE_GRADE)
                                        && bean.tables.T_NOTICE.get(j).CODE_COLOR.equals(updateBean.tables.T_NOTICE.get(i).CODE_COLOR)
                                ) {
                                    updateBean.tables.T_NOTICE.get(i).SCAN_WEIGHT = bean.tables.T_NOTICE.get(j).SCAN_WEIGHT;
                                }
                            }


                        }
                        bean.tables.T_NOTICE = updateBean.tables.T_NOTICE;
                        adapter.setList(bean.tables.T_NOTICE);
                        isFinish();
                        showToastMsg("更新完毕！");
                    }

                } else {
                    showToastMsg(updateBean.exports.E_MESSAGE);
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

 /*       JsonObject obj = new JsonObject();

        obj.addProperty("id", "test");
        obj.addProperty("password", "test");


        String route = new Gson().toJson(obj);
        System.out.println("aaaa" + route);
        LogUtils.e(route);*/

        SheetSubmitBean bean = new SheetSubmitBean();
        SheetSubmitBean.ImportsBean childbean = new SheetSubmitBean.ImportsBean();
        childbean.I_DELIVERY_BILL = madanhao;

        bean.imports = childbean;
        String route = new Gson().toJson(bean);
        LogUtils.e(route);
        RequestBody body = RequestBody.create(MediaType.parse("text/plan; charset=utf-8"), route);
        Observable observable = ShuangTuMethods.getRetrofit("rfcs/").create(ShuangTuService.class).
                ZHY_FN_ST_MARK_BARCODE_NOTICE(body);
        ShuangTuMethods.getInstance().toOtherSubscribe(observable, subscriber);
        rxManager.add(subscriber);
    }

    private void setRightNavigationText() {
        m_navigationBar.setVisibility(View.VISIBLE);
        WidgetButton widgetButton = new WidgetButton(this, R.string.scan_delete);
        widgetButton.getTextView().setTextColor(getResources().getColor(R.color.white));
        widgetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tree.size() == 0) {
                    showToastMsg("请先扫描");
                } else {
                    gotoActivity(ScanDeleteActivity.class);
                }
            }
        });
        m_navigationBar.setRightMenu(widgetButton);
    }

    private void gotoSubmit() {
        String route1 = new Gson().toJson(scanIntrueBean);
        LogUtils.e(route1);
        Intent intent = new Intent(context,Tes.class);
        intent.putExtra(Tes.INFO,route1);
        startActivity(intent);
        showProgressingDialog();
        CommonSubscriber<SubmitListBagsBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                SubmitListBagsBean submitListBagsBean = (SubmitListBagsBean) o;
                if (EmptyUtils.isNotEmpty(submitListBagsBean) && submitListBagsBean.exports.E_RETURN.equals("1")) {
                    showToastMsg("提交完成");
//                    SPUtils.setShareString(madanhao + "query", "");
//                    SPUtils.setShareString(madanhao + "submit", "");
                    onBackPressed();
                } else {
                    showToastMsg(submitListBagsBean.exports.E_MESSAGE);
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        String route = new Gson().toJson(scanIntrueBean);
        LogUtils.e(route);


        RequestBody body = RequestBody.create(MediaType.parse("text/plan; charset=utf-8"), route);
        Observable observable = ShuangTuMethods.getRetrofit("rfcs/").create(ShuangTuService.class).
                ZHY_FN_ST_MARK_BARCODE_UPLOAD(body);
        ShuangTuMethods.getInstance().toOtherSubscribe(observable, subscriber);
        rxManager.add(subscriber);
    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tvSubmit);
        bean = (QueryResultBean) getIntent().getSerializableExtra(BEAN);
        madanhao = getIntent().getStringExtra(MADANNUM);

        if (EmptyUtils.isNotEmpty(bean)) {
            tablesBeans = new ScanIntrueBean.TablesBean();
            tablesBeans.T_BARCODE = new ArrayList<ScanIntrueBean.TablesBean.TBARCODEBean>();
        } else {  // 来自保存的
            tablesBeans = new ScanIntrueBean.TablesBean();
            tablesBeans = new Gson().fromJson(SPUtils.getShareString(madanhao + "submit"), tablesBeans.getClass());
            bean = new QueryResultBean();
            bean = new Gson().fromJson(SPUtils.getShareString(madanhao + "query"), bean.getClass());
            //初始化tree
            for (int i = 0; i < tablesBeans.T_BARCODE.size(); i++) {
                tree.add(tablesBeans.T_BARCODE.get(i).BARCODE);
            }
            isFinish();
        }
        attachClickListener(tvUpdate);


        openScanEnable();
        scanOutputDisable();
        scanDataBroadcastReceiver = new ScanDataBroadcastReceiver(new ScanDataBroadcastReceiver.PdaCallBack() {
            @Override
            public void setinfo(String s) {
                if (isAccept) {
//                    if (tvSubmit.isEnabled()) {
//                        showToastMsg("扫描已完成");
//                        return;
//                    }
                    if (hasScan(s)) {
                        showToastMsg("请不要重复扫码！");
                    } else { // 没有扫过
                        gotoModifyItem(s);
                    }
                }

            }
        });
        setScanReceiver(scanDataBroadcastReceiver);
        setReclerview();
        if (EmptyUtils.isNotEmpty(btnBack2)) {
            btnBack2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showTwoDialog("提示", "还没有完成扫描，是否全部删除？"
                            , "取消", "确认");
                    setOnDialogViewClick(new OnDialogViewClick() {
                        @Override
                        public void leftViewClick() {  // 左边

                        }

                        @Override
                        public void rightViewClick() { // 右边


                            SPUtils.setShareString(madanhao + "query", "");
                            SPUtils.setShareString(madanhao + "submit", "");
                            onBackPressed();
                        }
                    });
                }
            });
        }
        deleObsver();
        gotoUpdate();

    }

    private void deleObsver() {
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SCAN_DELETE) && EmptyUtils.isNotEmpty((String) rxKeyEvent.getValue())) {
                    // 要删除的箱包
                    String deleBagsNum = (String) rxKeyEvent.getValue();
                    System.out.println("deleBagsNum" + deleBagsNum);
                    if (hasScanDelete(deleBagsNum)) {  // 当前扫过才可以进行操作
                        gotoDeleteItem(deleBagsNum);
                    } else {
                        showToastMsg("非法删除");
                    }

                }
            }
        }));

    }

    private void gotoModifyItem(String s) {
        if (EmptyUtils.isEmpty(s) || s.length() != 21 || EmptyUtils.isEmpty(bean)
                || EmptyUtils.isEmpty(bean.tables)
                || EmptyUtils.isEmpty(bean.tables.T_NOTICE)
                || bean.tables.T_NOTICE.size() == 0
        ) {
            showToastMsg("数据发生错误，请更新数据后再次扫码");
            return;
        }
        String CODE_SPEC = s.substring(7, 10); //规格
        String CODE_BATCH = s.substring(10, 13); //批号
        String CODE_GRADE = s.substring(13, 14); //等级
        String CODE_COLOR = s.substring(14, 16); //颜色
        double zheng = Double.valueOf(s.substring(16, 19));
        double xiao = Double.valueOf(s.substring(19, 21)) / 100 * 1.0;
        String scanWeight = NumberUtils.keepTwo((zheng + xiao));
        System.out.println("规格" + CODE_SPEC);
        System.out.println("批号" + CODE_BATCH);
        System.out.println("等级" + CODE_GRADE);
        System.out.println("颜色" + CODE_COLOR);
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).CODE_SPEC)
                    || EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).CODE_BATCH)
                    || EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).CODE_GRADE)
                    || EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).CODE_COLOR)
            ) {
                showToastMsg("数据发生错误，请更新数据后再次扫码");
                return;
            }
            if (CODE_SPEC.equals(bean.tables.T_NOTICE.get(i).CODE_SPEC)
                    && CODE_BATCH.equals(bean.tables.T_NOTICE.get(i).CODE_BATCH)
                    && CODE_GRADE.equals(bean.tables.T_NOTICE.get(i).CODE_GRADE)
                    && CODE_COLOR.equals(bean.tables.T_NOTICE.get(i).CODE_COLOR)
            ) {

                if (EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT)) { // 第一次扫描
                    tree.add(s);
                    showToastMsg("扫码成功！");
                    bean.tables.T_NOTICE.get(i).SCAN_WEIGHT = scanWeight;
                    adapter.setList(bean.tables.T_NOTICE);
                    tvYisao.setText("已扫" + tree.size() + "包");
                    ScanIntrueBean.TablesBean.TBARCODEBean item = new ScanIntrueBean.TablesBean.TBARCODEBean();
                    item.BARCODE = s;
                    item.SPLIT = "";
                    item.VBELN = madanhao;
                    tablesBeans.T_BARCODE.add(item);

                    isFinish();

                    return;
                } else {
                    if(CODE_GRADE.equals("A")){
                        weightHasMore(Double.valueOf(scanWeight),s);
                    }else {
                        tree.add(s);
                    }
                    showToastMsg("扫码成功！");
                    bean.tables.T_NOTICE.get(i).SCAN_WEIGHT = NumberUtils.keepTwo((Double.valueOf(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT) + Double.valueOf(scanWeight)));
                    adapter.setList(bean.tables.T_NOTICE);
                    tvYisao.setText("已扫" + tree.size() + "包");
                    ScanIntrueBean.TablesBean.TBARCODEBean item = new ScanIntrueBean.TablesBean.TBARCODEBean();

                    item.BARCODE = s;
                    item.VBELN = madanhao;

                    tablesBeans.T_BARCODE.add(item);
                    isFinishSave();
                    return;
                }
            }
        }
        showToastMsg("扫错箱包！");
        return;
    }


    private void gotoDeleteItem(String s) {
        String CODE_SPEC = s.substring(7, 10); //规格
        String CODE_BATCH = s.substring(10, 13); //批号
        String CODE_GRADE = s.substring(13, 14); //等级
        String CODE_COLOR = s.substring(14, 16); //颜色
        double zheng = Double.valueOf(s.substring(16, 19));
        double xiao = Double.valueOf(s.substring(19, 21)) / 100 * 1.0;
        String scanWeight = NumberUtils.keepTwo((zheng + xiao));
        System.out.println("规格" + CODE_SPEC);
        System.out.println("批号" + CODE_BATCH);
        System.out.println("等级" + CODE_GRADE);
        System.out.println("颜色" + CODE_COLOR);
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (CODE_SPEC.equals(bean.tables.T_NOTICE.get(i).CODE_SPEC)
                    && CODE_BATCH.equals(bean.tables.T_NOTICE.get(i).CODE_BATCH)
                    && CODE_GRADE.equals(bean.tables.T_NOTICE.get(i).CODE_GRADE)
                    && CODE_COLOR.equals(bean.tables.T_NOTICE.get(i).CODE_COLOR)
            ) {

                if (EmptyUtils.isEmpty(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT)) {
                 /*   bean.tables.T_NOTICE.get(i).SCAN_WEIGHT = scanWeight;
                    adapter.setList(bean.tables.T_NOTICE);
                    tvYisao.setText("已扫" + tree.size() + "包");
                    ScanIntrueBean.TablesBean.TBARCODEBean item = new ScanIntrueBean.TablesBean.TBARCODEBean();
                    item.BARCODE = s;
                    item.SPLIT = "";
                    item.VBELN = bean.tables.T_NOTICE.get(0).NO;
                    tablesBeans.T_BARCODE.add(item);
                    isFinish();
                    break;*/
                } else {
                    bean.tables.T_NOTICE.get(i).SCAN_WEIGHT = NumberUtils.keepTwo((Double.valueOf(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT) - Double.valueOf(scanWeight)));
                    adapter.setList(bean.tables.T_NOTICE);
                    tvYisao.setText("已扫" + tree.size() + "包");
                    ScanIntrueBean.TablesBean.TBARCODEBean item = new ScanIntrueBean.TablesBean.TBARCODEBean();
                    item.BARCODE = s;
                    item.VBELN = bean.tables.T_NOTICE.get(0).NO;
                    tablesBeans.T_BARCODE.add(item);
                    // 删除
                    for (Iterator<ScanIntrueBean.TablesBean.TBARCODEBean> ite = tablesBeans.T_BARCODE.iterator(); ite.hasNext(); ) {
                        ScanIntrueBean.TablesBean.TBARCODEBean tnoticeBean = ite.next();
                        System.out.println(tnoticeBean);
                        if (tnoticeBean.BARCODE.contains(s)) {
                            ite.remove();
                        }
                    }
                    showToastMsg("删除成功！");
                    break;
                }
            }
        }
    }

    private void isFinish() {
        double need = 0.00;
        double scanWeight = 0.00;
        if (EmptyUtils.isEmpty(bean)
                || EmptyUtils.isEmpty(bean.tables)
                || EmptyUtils.isEmpty(bean.tables.T_NOTICE)
                || bean.tables.T_NOTICE.size() == 0
        ) {
            return;
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).WEIGHT)) {
                need += Double.valueOf(bean.tables.T_NOTICE.get(i).WEIGHT);
            }
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT)) {
                scanWeight += Double.valueOf(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT);
            }
        }
        if (NumberUtils.keepTwo(need).equals(NumberUtils.keepTwo(scanWeight))) {
            tvSubmit.setEnabled(true);
        } else {
            tvSubmit.setEnabled(false);
        }
        tvYisao.setText("已扫" + tree.size() + "包");

    }
    private void isFinishSave() {
        double need = 0.00;
        double scanWeight = 0.00;
        if (EmptyUtils.isEmpty(bean)
                || EmptyUtils.isEmpty(bean.tables)
                || EmptyUtils.isEmpty(bean.tables.T_NOTICE)
                || bean.tables.T_NOTICE.size() == 0
        ) {
            return;
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).WEIGHT)) {
                need += Double.valueOf(bean.tables.T_NOTICE.get(i).WEIGHT);
            }
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT)) {
                scanWeight += Double.valueOf(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT);
            }
        }
        if (NumberUtils.keepTwo(need).equals(NumberUtils.keepTwo(scanWeight))) {
            tvSubmit.setEnabled(true);
        } else {
            tvSubmit.setEnabled(false);
        }
        if (tree.size() != 0) {
            String query = new Gson().toJson(bean);
            String submit = new Gson().toJson(tablesBeans);
            SPUtils.setShareString(madanhao + "query", query);
            SPUtils.setShareString(madanhao + "submit", submit);
        }
        tvYisao.setText("已扫" + tree.size() + "包");

    }

    private void weightHasMore(double  scanWeiht,String s) {  // 超过扫描重量 不让保存 不修改参数
        double need = 0.00;
        double scanWeight = 0.00;
        if (EmptyUtils.isEmpty(bean)
                || EmptyUtils.isEmpty(bean.tables)
                || EmptyUtils.isEmpty(bean.tables.T_NOTICE)
                || bean.tables.T_NOTICE.size() == 0
        ) {
            return;
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).WEIGHT)) {
                need += Double.valueOf(bean.tables.T_NOTICE.get(i).WEIGHT);
            }
        }
        for (int i = 0; i < bean.tables.T_NOTICE.size(); i++) {
            if (EmptyUtils.isNotEmpty(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT)) {
                scanWeight += Double.valueOf(bean.tables.T_NOTICE.get(i).SCAN_WEIGHT);
            }
        }
        scanWeight+= scanWeiht ;
        if(scanWeight>need){
            showToastMsg("重量超出");
            return;
        }else { // 保存 todo
            tree.add(s);

        }
        if (NumberUtils.keepTwo(need).equals(NumberUtils.keepTwo(scanWeight))) {
            tvSubmit.setEnabled(true);
        } else {
            tvSubmit.setEnabled(false);
        }
        tvYisao.setText("已扫" + tree.size() + "包");

    }

    private boolean hasScan(String s) {
        Iterator iter = tree.iterator();
        while (iter.hasNext()) {
            String temp = (String) iter.next();
            if (temp.equals(s)) {
                return true;
            }
//            System.out.println("-------" + iter.next() + "------");
        }

        return false;
    }


    private boolean hasScanDelete(String s) {
        Iterator iter = tree.iterator();
        while (iter.hasNext()) {
            String temp = (String) iter.next();
            if (temp.equals(s)) {
                iter.remove();
                return true;
            }
//            System.out.println("-------" + iter.next() + "------");
        }

        return false;
    }

    private void setReclerview() {
        adapter = new ScanAdapter(context, bean.tables.T_NOTICE);
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 10), true));
        recyclerview.setRefreshEnabled(false);
        recyclerview.setLoadMoreEnabled(false);
        recyclerview.setHasFixedSize(true);
        recyclerview.setAdapter(adapter);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("扫包确认");
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            showTwoDialog("提示", "还没有完成扫描，是否保存并且退出？"
                    , "取消", "确认");
            setOnDialogViewClick(new OnDialogViewClick() {
                @Override
                public void leftViewClick() {  // 左边

                }

                @Override
                public void rightViewClick() { // 右边
                    if (tree.size() != 0) {
                        String query = new Gson().toJson(bean);
                        String submit = new Gson().toJson(tablesBeans);
                        SPUtils.setShareString(madanhao + "query", query);
                        SPUtils.setShareString(madanhao + "submit", submit);
                    }

                    onBackPressed();
                }
            });

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("kkkkkkll---");
        getToken();
    }

    private void getToken() {

        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                String token = (String) o;
//                showToastMsg(token);
                SPUtils.setShareString(DataCenter.TOKEN, token);
                DataCenter.getInstance().setToken(token);

            }

            @Override
            public void onError(String e, int code) {

                showToastMsg(e);
            }
        });

        JsonObject obj = new JsonObject();

        obj.addProperty("id", "sap-test");
        obj.addProperty("password", "sap-test-password");

        String route = new Gson().toJson(obj);
        System.out.println("aaaa" + route);
        LogUtils.e(route);
        RequestBody body = RequestBody.create(MediaType.parse("text/plan; charset=utf-8"), route);
        Observable observable = ShuangTuMethods.getRetrofitToken("http://192.168.0.249:9998/api/").create(ShuangTuService.class).
                auth(body);
        ShuangTuMethods.getInstance().toOtherSubscribe(observable, subscriber);
        rxManager.add(subscriber);
    }
}
